package com.hthy.futures.utils;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
   *
   * Author:  Mr.bingley
   * Version:
   * Date:  2017/3/27
   */

public class DataUtils {
    static SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm");

   // public static SimpleDateFormat minSdf = new SimpleDateFormat("0000");
    public static DecimalFormat minSdf = new DecimalFormat("0000");

    public static SimpleDateFormat xFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * 将时间戳转换为时间
     * @param s
     * @return
     */
    public static String stampToDate(String s){
        String res;
        long lt = new Long(s+"000");        // 得加入三个零不然不行
        Date date = new Date(lt);
        res = xFormate.format(date);
        return res;
    }



    /**
     * 比较两者时间
     * @param DATE1
     * @param DATE2
     * @return
     */
    public static int compare_date(String DATE1, String DATE2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {   // 大于
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {  // 小于
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;  // 相等
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    /**
     * 把时间格式转为毫秒  (这里要求是十位，故除以1000）
     * @param text
     * @return
     * @throws ParseException
     */
    public static long parseDate(String text)
            throws ParseException
    {
        // yyyyMMddhhmm
        long millionSeconds = sdf.parse(text).getTime();//毫秒
        //LogUtils.e("bb",""+ millionSeconds);
        return millionSeconds/1000;
    }


    // 获取今天
    public static String getToday() {
        Date date = new Date();
        LogUtils.e("gettoday"+formatter.format(date));
        return formatter.format(date);
    }

    public static String getTomorrow() {
        Date date=new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
        Date date1=calendar.getTime(); //这个时间就是日期往后推一天的结果
        return formatter.format(date1);
    }

    // 单日

    // 2015-1-1

    // 本月



    //System.out.println("当天0点时间："+ getTimesmorning().toLocaleString());
//System.out.println("当天24点时间："+ getTimesnight().toLocaleString());

  //  System.out.println("本月初0点时间："+ getTimesMonthmorning().getTime());
  //  System.out.println("本月未24点时间："+ getTimesMonthnight().getTime());

    // 近三月
    public static String getThreeTime() {
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(calendar.MONTH, -3);  //设置为前3月
        dBefore = calendar.getTime();   //得到前3月的时间
        String defaultStartDate = formatter.format(dBefore);    //格式化前3月的时间
        String defaultEndDate = formatter.format(dNow); //格式化当前时间
        LogUtils.e("前3个月的时间是：" + defaultStartDate+"\n"+"今天生成的时间是：" + defaultEndDate);
        return  defaultStartDate;
    }

    // 获得当天0点时间
    public static String getTimesmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        LogUtils.e(formatter.format(cal.getTime()));
        return formatter.format(cal.getTime());
    }


    // 获得当天24点时间
    public static String getTimesnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 24);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        LogUtils.e(formatter.format(cal.getTime()));
        return  formatter.format(cal.getTime());
    }

    // 获得本月第一天0点时间
    public static String getTimesMonthmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        LogUtils.e(formatter.format(cal.getTime()));
        return  formatter.format(cal.getTime());
    }


    // 获得本月最后一天24点时间
    public static String getTimesMonthnight() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal.set(Calendar.HOUR_OF_DAY, 24);
        LogUtils.e(formatter.format(cal.getTime()));
        return formatter.format(cal.getTime());
    }
}
